/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Output;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

import Core.*;
/**
 *
 * @author harish
 */
public class Reporting 
{
    
    void generate()
    {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(Configuration.GetInstance().GetURL(),Configuration.GetInstance().GetUser(),Configuration.GetInstance().GetPwd());
            System.out.println("Loading Report Designs");
            InputStream input = new FileInputStream(Configuration.GetInstance().GetOutputJRXML());
            JasperDesign jasperDesign = JRXmlLoader.load(input);

            System.out.println("Compiling Report Designs");
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

            System.out.println("Creating JasperPrint Object");
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("ReportTitle", "PDF JasperReport");

            System.out.println("Filling Report to File");
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);

            //Exporting the report
            OutputStream output = new FileOutputStream(Configuration.GetInstance().GetOutputPath()+ "\\catalog1.pdf");
            JasperExportManager.exportReportToPdfStream(jasperPrint, output);
            JasperViewer.viewReport(jasperPrint);

            System.out.println("Report Generation Complete");
            conn.close();
       } 
       catch (Exception e) 
       {
           
       }
   }
}
